from numpy import zeros
import random
from Tkinter import *
import threading
import time


def InitTk(Tab, size):

    k=0
    for i in range(0,size):
        for j in range(0,size):
            if Tab[i][j]==1 :
                Cell[k]=Canvas(Grille, bg='light grey', height=500/size, width=500/size)
            else:
                Cell[k]=Canvas(Grille, bg='white', height=500/size, width=500/size)
            Cell[k].grid(row=i, column=j)
            Cell[k].bind("<Button-1>", pointeur)
            Cell[k].i, Cell[k].j = i, j
            k+=1

def Afficher(Tab, size):

    k=0
    for i in range(0,size):
        for j in range(0,size):
            if Tab[i][j]==1 :
                Cell[k].configure(bg='light grey')
            else:
                Cell[k].configure(bg='white')
            k+=1

def Condition(sum, Tableau):
    condition = 0
    for ind, val in enumerate(Tableau):
        if val==1 and sum==ind :
            condition+=1
    return condition

def Suivant(Tab, Tabn, size, birth, death, cyclique):

    if cyclique ==0 :
        for i in range(0,size):
            for j in range(0,size):
                sum = 0
                if i<size-1 :
                    sum = sum + Tab[i+1][j]
                    if (j>0) : sum = sum + Tab[i+1][j-1]
                    if (j+1<size) : sum = sum + Tab[i+1][j+1]
                if (i>0) :
    				sum = sum + Tab[i-1][j]
    				if (j>0) : sum = sum + Tab[i-1][j-1]
    				if (j+1<size)  : sum = sum + Tab[i-1][j+1]
                if (j+1<size) : sum = sum + Tab[i][j+1]
                if (j-1>=0) : sum = sum + Tab[i][j-1]
                if (Tab[i][j] == 1):
                    condition=Condition(sum,death)
                    if not condition :
                        Tabn[i][j] = 0
                elif (Tab[i][j] == 0):
                    condition=Condition(sum,birth)
                    if condition : Tabn[i][j] = 1

    if cyclique==1 :
        for i in range(0,size):
            for j in range(0,size):
                sum = Tab[(i+1) % size][j] + Tab[(i-1+size) % size][j] + Tab[i][(j-1+size) % size] + Tab[i][(j+1) % size]+ Tab[(i+1) % size][(j+1) % size]+ Tab[(i-1+size) % size][(j+1) % size]+ Tab[(i+1) % size][(j-1+size) % size]+ Tab[(i-1+size) % size][(j-1+size) % size]
                if (Tab[i][j] == 1):
                    condition=Condition(sum,death)
                    if not condition :
                        Tabn[i][j] = 0
                elif (Tab[i][j] == 0):
                    condition=Condition(sum,birth)
                    if condition : Tabn[i][j] = 1
    return Tabn

def Aleatoire(Tab, size) :
    for i in range(0,size):
        for j in range (0,size):
            Tab[i][j]=random.randint(1,10) % 2
    return Tab

def NouvelleGrille():
    global Tab, size
    Aleatoire(Tab, size)
    InitTk(Tab,size)

def Step():
    global Tab,Tabn,size,birth,death,cyclique
    for i in range(0,size):
        for j in range(0,size):
            Tabn[i][j]=Tab[i][j]

    Afficher(Tabn,size)
    Suivant(Tab,Tabn,size,birth,death,cyclique)
    print ""
    Afficher(Tabn,size)

    for i in range(0,size):
        for j in range(0,size):
            Tab[i][j]=Tabn[i][j]

def Options():
    global cyclique
    fen2= Tk()
    fen2.title('Options')
    Checkbutton(fen2, text="Cyclique", variable=cyclique).pack()


def pointeur(event):
    global Tab
    i,j = event.widget.i, event.widget.j
    if event.widget["background"]=="white":
        event.widget.configure(bg="light grey")
        Tab[i][j]=1
    else:
        event.widget.configure(bg="white") #change la couleur
        Tab[i][j]=0

def Lancer():
    global stop
    while not stop:
        Step()
        time.sleep(0.01)

def Vider():
    global Tab
    Tab=zeros((size,size),int)
    Afficher(Tab,size)

size=10
birth=[0,0,0,1,0,0,0,0]
death=[0,0,1,1,0,0,0,0]
cyclique=1 #options par defaut

fen1= Tk()
fen1.title('Jeu de la vie')
Grille = Frame(fen1, width =500, height =500, bg="light yellow")
Grille.pack()



Tab=zeros((size,size),int)
Tabn=zeros((size,size),int)

Cell=[""]
for i in range(0,(size*size)-1):
    Cell.append("") #initlisation du tableau d'affichage

NouvelleGrille()

def Stop():
    global stop
    stop=True
    but.configure(text="Launch", command=boucle)



def boucle():
    global stop
    stop=False
    but.configure(text="Stop", command=Stop)
    thread=threading.Thread(group=None, target=Lancer, name=None, args=(), kwargs={})
    thread.start()




Button(fen1, text="Etape suivante", width=30, relief=GROOVE, command=Step).pack()
but = Button(fen1, text="Lancer", width=30, relief=GROOVE, command=boucle)
but.pack()
Button(fen1, text="Options", width=30, relief=GROOVE, command=Options).pack()
Button(fen1, text="Nouvelle Grille", width=30, relief=GROOVE, command=NouvelleGrille).pack()
Button(fen1, text="Effacer Grille", width=30, relief=GROOVE, command=Vider).pack()
Button(fen1, text="Quitter le jeu de la vie", width=30, relief=GROOVE, command=fen1.destroy).pack()


fen1.mainloop()

"""FullScreen
r = Tk()
r.geometry(str(r.winfo_screenwidth())+"x"+str(r.winfo_screenheight())+"+0+0")
r.overrideredirect(TRUE)
r.mainloop()
"""